package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.RewardPunishment;
import com.dnjn.happiness.biz.service.IAppraiseService;
import com.dnjn.happiness.biz.service.IRewardPunishmentService;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.HxBeanSeed;
import com.dnjn.happiness.common.core.model.ModelHappinessScores;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.RemoteEmoUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 奖惩Controller
 *
 * @author happiness
 * @date 2023-10-19
 */
@Tag(name = "biz punishment 奖惩", description = "奖惩")
@RestController
@RequestMapping("/punishment")
public class RewardPunishmentController extends BaseController {
    @Autowired
    private IRewardPunishmentService rewardPunishmentService;

    @Autowired
    private RemoteEmoUserService hxBeanService;

    @Autowired
    private IAppraiseService appraiseService;

    /**
     * 查询奖惩列表
     */
    @RequiresPermissions("biz:punishment:list")
    @PostMapping("/list1")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult list1(@RequestBody RewardPunishment rewardPunishment, @Parameter(required = true) @RequestParam(defaultValue = "false") Boolean fromFront, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<RewardPunishment> page = Page.of(current, size);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", rewardPunishment.getUserId());
        map.put("deptId", rewardPunishment.getDeptId());
        map.put("xtype", rewardPunishment.getXtype());
        if (fromFront) {
            map.put("etype", "NEGATIVE");
        }
        Page<RewardPunishment> list = rewardPunishmentService.getPage(page, map);
        return success(list);
    }

    /**
     * 查询奖惩列表
     */
//    @RequiresPermissions("biz:punishment:list")
    @Log(title = "biz", businessType = BusinessType.LIST)
    @PostMapping("/list")
    public AjaxResult list(@RequestBody RewardPunishment rewardPunishment, @Parameter(required = true) @RequestParam(defaultValue = "false") Boolean fromFront, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<RewardPunishment> page = Page.of(current, size);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", rewardPunishment.getUserId());
        map.put("deptId", rewardPunishment.getDeptId());
        map.put("xtype", rewardPunishment.getXtype());
        if (fromFront) {
            map.put("etype", "NEGATIVE");
        }
        Page<RewardPunishment> list = rewardPunishmentService.getPage(page, map);
        return success(list);
    }

    /**
     * 导出奖惩列表
     */
    @Log(title = "奖惩", businessType = BusinessType.EXPORT)
    @RequiresPermissions("biz:punishment:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, RewardPunishment rewardPunishment) {
        List<RewardPunishment> list = rewardPunishmentService.list(Wrappers.query(rewardPunishment));
        ExcelUtil<RewardPunishment> util = new ExcelUtil<RewardPunishment>(RewardPunishment.class);
        util.exportExcel(response, list, "奖惩数据");
    }

    /**
     * 获取奖惩详细信息
     */
    @GetMapping(value = "/get/{id}")
    @RequiresPermissions("biz:punishment:query")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getRewardPunishment(@PathVariable("id") Long id) {
        return success(rewardPunishmentService.getById(id));
    }


    /**
     * wx获取奖惩详细信息
     */
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(rewardPunishmentService.getById(id));
    }

    /**
     * 新增奖惩
     */
    @Log(title = "奖惩", businessType = BusinessType.INSERT)
    @PostMapping("/add1")
    @RequiresPermissions("biz:punishment:add")
    public AjaxResult add1(@RequestBody RewardPunishment rewardPunishment) {
        rewardPunishment.setCreateBy(SecurityUtils.getUsername());
        boolean flag = rewardPunishmentService.save(rewardPunishment);
        if (flag) {
            //更新快乐豆
            if ("REWARD".equals(rewardPunishment.getXtype())) {
                HxBeanSeed beanSeed = new HxBeanSeed();
                beanSeed.setUserId(rewardPunishment.getUserId());
                beanSeed.setRiskType(Constants.REWARD_HLBEAN_CODE);
                R<Boolean> result = hxBeanService.updateHBean(beanSeed, SecurityConstants.INNER);
            }
        }
        return toAjax(flag);
    }

    /**
     * wx新增奖惩
     */
    @Log(title = "奖惩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RewardPunishment rewardPunishment) {
        rewardPunishment.setCreateBy(SecurityUtils.getUsername());
        boolean flag = rewardPunishmentService.save(rewardPunishment);
        if (flag) {
            //更新快乐豆
            if ("REWARD".equals(rewardPunishment.getXtype())) {
                HxBeanSeed beanSeed = new HxBeanSeed();
                beanSeed.setUserId(rewardPunishment.getUserId());
                beanSeed.setRiskType(Constants.REWARD_HLBEAN_CODE);
                R<Boolean> result = hxBeanService.updateHBean(beanSeed, SecurityConstants.INNER);
            }
        }
        return toAjax(flag);
    }

    /**
     * 修改奖惩
     */
    @Log(title = "奖惩", businessType = BusinessType.UPDATE)
    @RequiresPermissions("biz:punishment:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody RewardPunishment rewardPunishment) {
        return toAjax(rewardPunishmentService.updateById(rewardPunishment));
    }

    /**
     * 删除奖惩
     */
    @Log(title = "奖惩", businessType = BusinessType.DELETE)
    @RequiresPermissions("biz:punishment:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            rewardPunishmentService.lambdaUpdate().eq(RewardPunishment::getId, id).set(RewardPunishment::getDeleted, "1").update();
        }
        return success();
    }


    @Operation(summary = "小程序根据用户Id查询用户奖惩记录列表")
    @GetMapping("/getRewardPunishmentList/{userId}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getRewardPunishmentList(@PathVariable Long userId) {
        Assert.notNull(userId, "用户ID不能为空");
        List<RewardPunishment> list = rewardPunishmentService.getRewardPunishmentList(userId);
        return AjaxResult.success(list);
    }

    /**
     * 获取奖惩记录消极记录数-积极记录数
     */
    @Operation(summary = "获取奖惩记录消极记录数-积极记录数")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    @GetMapping("/getRewardPunishmenTotal/{userId}")
    public AjaxResult getRewardPunishmenTotal(@PathVariable Long userId) {
        Assert.notNull(userId, "用户ID不能为空");
        List<ModelHappinessScores> list = appraiseService.getAppraiseHappinessScores(userId, "biz_user_reward_punishment");
        return AjaxResult.success(list);
    }
}
